'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  const { deviceId } = event;

  if (!deviceId) {
    return {
      code: -1,
      message: '设备ID不能为空'
    };
  }

  try {
    // 获取单个设备信息
    const device = await db.collection('devices')
      .doc(deviceId)
      .field({
        '_id': true,
        'de_name': true,
        'de_model': true,
        'de_location': true,
        'de_status': true,
        'de_data': true,
        'create_time': true,
        'update_time': true
      })
      .get();

    console.log('查询到的设备数据:', device.data);

    if (device.data && device.data.length > 0) {
      return {
        code: 0,
        message: '获取成功',
        data: device.data[0]
      };
    } else {
      return {
        code: -1,
        message: '设备不存在'
      };
    }
  } catch (e) {
    console.error('获取设备信息失败:', e);
    return {
      code: -1,
      message: '获取设备信息失败',
      error: e.message
    };
  }
}; 